import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView/index.vue'
import Layout from '../views/Layout.vue'
import LoginInfo from '../views/LoginInfo/index.vue'
import { useLoginStore } from '@/stores/loginStore'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'layout',
      component: Layout,
      children: [
        {
          path: '/',
          name: 'home',
          component: HomeView,
          meta: {
            reqireAuth: true,
            title: '首页'
          }
        },
        {
          path: '/projectInfo',
          name: 'projectInfo',
          component: () => import('../views/ProjectInfo/index.vue'),
          meta: {
            reqireAuth: true,
            title: '项目基础信息'
          }
        },
        {
          path: '/tunnelInfo',
          name: 'tunnelInfo',
          component: () => import('../views/TunnelInfo/index.vue'),
          meta: {
            reqireAuth: true,
            title: '隧道基础信息'
          }
        },
        {
          path: '/buildManage',
          name: 'buildManage',
          component: () => import('../views/BuildManage/index.vue'),
          meta: {
            reqireAuth: true,
            title: '施工监控检测'
          },
          children: [
            {
              path: '/plan',
              name: 'plan',
              component: () => import('../views/BuildManage/Plan/index.vue'),
              meta: {
                reqireAuth: true,
                title: '检测计划'
              }
            },
            {
              path: '/section',
              name: 'section',
              component: () => import('../views/BuildManage/Section/index.vue'),
              meta: {
                reqireAuth: true,
                title: '切面检测'
              }
            }
          ]
        },
        {
          path: '/geologicalInfo',
          name: 'geologicalInfo',
          component: () => import('../views/GeologicalInfo/index.vue'),
          meta: {
            reqireAuth: true,
            title: '超前地质预报'
          }
        },
        {
          path: '/systemManage',
          name: 'systemManage',
          component: () => import('../views/SystemManage/index.vue'),
          meta: {
            reqireAuth: true,
            title: '系统信息管理'
          }
        },
        {
          path: '/userCenter',
          name: 'userCenter',
          component: () => import('../views/UserCenter/index.vue'),
          meta: {
            reqireAuth: true,
            title: '用户中心'
          }
        }
      ],
    },
    {
      path: '/login',
      name: 'login',
      component: LoginInfo,
    },
    {
      path: '/:pathMatch(.*)*',
      name: 'NotFound',
      component: () => import('@/views/NotFound/404.vue')
    }
  ],
})

// 守卫
router.beforeEach((to, from, next) => {
  if (to.meta.reqireAuth) {
    const loginStore = useLoginStore()
    if (loginStore.token) {
      next()
    } else {
      next({
        path: '/login',
        query: {
          redirect: to.fullPath
        }
      })
    }
  } else {
    next()
  }
})

export default router
